﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace UniRx.Operators
{
	// Token: 0x02000355 RID: 853
	internal class ZipObservable<T1, T2, T3, TR> : OperatorObservableBase<TR>
	{
		// Token: 0x06001184 RID: 4484 RVA: 0x000502E8 File Offset: 0x0004E6E8
		public ZipObservable(IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, ZipFunc<T1, T2, T3, TR> resultSelector) : base(source1.IsRequiredSubscribeOnCurrentThread<T1>() || source2.IsRequiredSubscribeOnCurrentThread<T2>() || source3.IsRequiredSubscribeOnCurrentThread<T3>())
		{
			this.source1 = source1;
			this.source2 = source2;
			this.source3 = source3;
			this.resultSelector = resultSelector;
		}

		// Token: 0x06001185 RID: 4485 RVA: 0x0005033D File Offset: 0x0004E73D
		protected override IDisposable SubscribeCore(IObserver<TR> observer, IDisposable cancel)
		{
			return new ZipObservable<T1, T2, T3, TR>.Zip(this, observer, cancel).Run();
		}

		// Token: 0x04000A4F RID: 2639
		private IObservable<T1> source1;

		// Token: 0x04000A50 RID: 2640
		private IObservable<T2> source2;

		// Token: 0x04000A51 RID: 2641
		private IObservable<T3> source3;

		// Token: 0x04000A52 RID: 2642
		private ZipFunc<T1, T2, T3, TR> resultSelector;

		// Token: 0x02000356 RID: 854
		private class Zip : NthZipObserverBase<TR>
		{
			// Token: 0x06001186 RID: 4486 RVA: 0x00050534 File Offset: 0x0004E934
			public Zip(ZipObservable<T1, T2, T3, TR> parent, IObserver<TR> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06001187 RID: 4487 RVA: 0x00050574 File Offset: 0x0004E974
			public IDisposable Run()
			{
				base.SetQueue(new ICollection[]
				{
					this.q1,
					this.q2,
					this.q3
				});
				IDisposable disposable = this.parent.source1.Subscribe(new ZipObserver<T1>(this.gate, this, 0, this.q1));
				IDisposable disposable2 = this.parent.source2.Subscribe(new ZipObserver<T2>(this.gate, this, 1, this.q2));
				IDisposable disposable3 = this.parent.source3.Subscribe(new ZipObserver<T3>(this.gate, this, 2, this.q3));
				return StableCompositeDisposable.Create(disposable, disposable2, disposable3, Disposable.Create(delegate
				{
					object obj = this.gate;
					lock (obj)
					{
						this.q1.Clear();
						this.q2.Clear();
						this.q3.Clear();
					}
				}));
			}

			// Token: 0x06001188 RID: 4488 RVA: 0x0005062D File Offset: 0x0004EA2D
			public override TR GetResult()
			{
				return this.parent.resultSelector(this.q1.Dequeue(), this.q2.Dequeue(), this.q3.Dequeue());
			}

			// Token: 0x06001189 RID: 4489 RVA: 0x00050660 File Offset: 0x0004EA60
			public override void OnNext(TR value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x0600118A RID: 4490 RVA: 0x00050670 File Offset: 0x0004EA70
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0600118B RID: 4491 RVA: 0x000506A8 File Offset: 0x0004EAA8
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000A53 RID: 2643
			private readonly ZipObservable<T1, T2, T3, TR> parent;

			// Token: 0x04000A54 RID: 2644
			private readonly object gate = new object();

			// Token: 0x04000A55 RID: 2645
			private readonly Queue<T1> q1 = new Queue<T1>();

			// Token: 0x04000A56 RID: 2646
			private readonly Queue<T2> q2 = new Queue<T2>();

			// Token: 0x04000A57 RID: 2647
			private readonly Queue<T3> q3 = new Queue<T3>();
		}
	}
}
